package com.tbeasy.financial;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.tbeasy.financial.data.TransactionItem;
import com.tbeasy.financial.provider.DatabaseUtil.TableTransaction;
import com.tbeasy.financial.util.Util;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AddTransactionActivity extends Activity {

	private static final int TRANSACTION_TYPE_INCOME = 1;
	private static final int TRANSACTION_TYPE_EXPENDITURE = 2;
	private static final int TIME_DIALOG = 10;

	private TextView timeView;
	private EditText amountText;
	private EditText memoText;
	private Button typeButton;
	private View timeTab;
	private Button saveButton;
	private Button againButton;
	private int currentType = TRANSACTION_TYPE_EXPENDITURE;

	private int mYear;
	private int mMonth;
	private int mDay;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_transaction);
		initView();
	}

	private void initView() {
		timeView = (TextView) findViewById(R.id.add_transaction_time);
		amountText = (EditText) findViewById(R.id.add_transaction_edittext);
		memoText = (EditText) findViewById(R.id.memo_text);
		timeTab = findViewById(R.id.add_transaction_time_tab);
		saveButton = (Button) findViewById(R.id.save_btn);
		againButton = (Button) findViewById(R.id.save_again_btn);
		typeButton = (Button) findViewById(R.id.add_transaction_type);
		timeView.setText(getTime(new Date()));

		typeButton.setOnClickListener(clickListener);
		timeTab.setOnClickListener(clickListener);
		saveButton.setOnClickListener(clickListener);
		againButton.setOnClickListener(clickListener);
	}

	private String getTime(Date date) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return dateFormat.format(date);
	}

	private OnClickListener clickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.add_transaction_type:
				currentType = currentType == TRANSACTION_TYPE_EXPENDITURE ? TRANSACTION_TYPE_INCOME
						: TRANSACTION_TYPE_EXPENDITURE;
				switchType(currentType);
				break;

			case R.id.add_transaction_time_tab:
				showDialog(TIME_DIALOG);
				break;

			case R.id.save_btn:
				saveTransaction();
				break;

			case R.id.save_again_btn:

				break;
			}
		}

	};

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case TIME_DIALOG:
			final Calendar c = Calendar.getInstance();
			return new DatePickerDialog(this, mDateSetListener, c.get(Calendar.YEAR), c.get(Calendar.MONTH),
					c.get(Calendar.DAY_OF_MONTH));
		}
		return null;
	}

	protected void saveTransaction() {

		long time = 0;
		if (mYear == 0) {
			time = System.currentTimeMillis();
		} else {
			time = Util.formatStringToDate(mYear + "-" + mMonth + "-" + (mDay + 1)).getTime();
		}
		int type = (Integer) typeButton.getTag();
		Double money = 0.0;
		try {
			money = Double.valueOf(amountText.getText().toString());
		} catch (Exception e) {
			amountText.requestFocus();
			Toast.makeText(this, "亲，金额好像不对吧", Toast.LENGTH_SHORT).show();
			return;
		}
		String memo = memoText.getText().toString();
		ContentValues values = new ContentValues();
		values.put(TableTransaction.CATEGORY_ID, type);
		values.put(TableTransaction.CREATE_TIME, time);
		values.put(TableTransaction.MEMO, memo);
		values.put(TableTransaction.BUYER_MONEY, money);
		Uri uri = getContentResolver().insert(TableTransaction.CONTENT_URI, values);
		if(uri != null) {
			Toast.makeText(this, "添加账目成功", Toast.LENGTH_SHORT).show();
			amountText.setText("");
			memoText.setText("");
		} else {
			Toast.makeText(this, "哦，失败了，再试一次吧！", Toast.LENGTH_SHORT).show();
		}
	}

	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
		public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
			mYear = year;
			mMonth = monthOfYear + 1;
			mDay = dayOfMonth;
		}
	};

	private void switchType(int type) {
		switch (type) {
		case TRANSACTION_TYPE_EXPENDITURE:
			typeButton.setText("新增开支");
			break;

		case TRANSACTION_TYPE_INCOME:
			typeButton.setText("新增收入");
			break;
		}
	}

}